package com.yenlien.traceyun.breed.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.breed.dao.BreedCropDetailDao;
import com.yenlien.traceyun.breed.dao.BreedDao;
import com.yenlien.traceyun.breed.dao.PreventDao;
import com.yenlien.traceyun.breed.entity.Breed;
import com.yenlien.traceyun.breed.service.BreedService;
import com.yenlien.traceyun.business.dao.BaseSoilDao;
import com.yenlien.traceyun.business.interceptor.DataAuth;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.rest.usercenter.UserRestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 养殖控制层
 *
 * @author maybe
 * @date 2020年9月23日
 */
@RestController
@RequestMapping("/breed")
public class BreedController extends BaseController {
    @Autowired
    private BreedService breedService;
    @Autowired
    private PreventDao preventDao;

    @Autowired
    private BreedDao breedDao;

    @Autowired
    private UserRestService userRestService;
    @Autowired
    private BaseSoilDao baseSoilDao;
    @Autowired
    private BreedCropDetailDao breedCropDetailDao;


    @GetMapping(value = "/breedCount")
    public Object breedCount() {
        Map<String, Integer> map = new HashMap<>();
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        JSONObject count = userRestService.findCountByCompanyId(companyId);
        Integer userCount = 0;
        if(count != null){
            userCount = count.getInteger("data");
        }
        map.put("农户信息", userCount);
        map.put("地块信息", baseSoilDao.breedBaseCount(UserAuthUtil.getLoginUser().getCompanyId()));
        map.put("RFID信息", breedCropDetailDao.countByCompId(UserAuthUtil.getLoginUser().getCompanyId()));
        return success(map);
    }


    //分页查询列表
    @DataAuth
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody JSONObject params) {
        Breed breed = new Breed();
        breed.setBreedName(params.getString("searchName"));
        breed.setBatch(params.getString("searchName"));
        breed.setPage(params.getIntValue("page"));
        Page<Breed> page = breedService.findByList(breed);
        if (page.getContent().size() > 0) {
            for (Breed breed1 : page.getContent()) {
                breed1.setPrevent(preventDao.findByBreedId(breed1.getId()).size());
            }
        }
        return buildPage(page);
    }

    //新增养殖
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody Breed breed) {
        breedService.save(breed);
        return success("success");
    }

    //绑定设备
    @RequestMapping(value = "/bind", method = RequestMethod.GET)
    public Object bind(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"code", "equipmentId", "equipmentName"});
        breedService.bind(params);
        return success("success");
    }

    //根据ID查询详情
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(breedService.findById(id));
    }


    //查询养殖批次号下拉
    @DataAuth
    @RequestMapping(value = "/findBatchSelect", method = RequestMethod.GET)
    public Object findBatchSelect(HttpServletRequest request) {
        String source = request.getParameter("source");
        if (StringUtils.isEmpty(source)) {
            return breedDao.findAllBatch();
        } else {
            if (source.equals("1")) {
                return breedDao.findTypeAllBatch("养殖");
            } else if (source.equals("2")) {
                return breedDao.findTypeAllBatch("繁殖");
            } else {
                return breedDao.findAllBatch();
            }
        }

    }
}
